package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class CartInfoController {
    @Autowired
    private CartFeignClient cartFeignClient;

    public static final String REDIRECTURL = "http://cart.gmall.com/toCart.html";
    //加入购物车
    @GetMapping("/addCart.html")
    public String cart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
        cartFeignClient.addToCart(skuId, skuNum);
        redirectAttributes.addAttribute("skuId",skuId);
        redirectAttributes.addAttribute("skuNum",skuNum);
//        cartInfo.setSkuNum(skuNum);
//        model.addAttribute("cartInfo",cartInfo);
//        return "cart/addCart";    解决刷新页面会重复添加商品的bug
        return "redirect:" + REDIRECTURL;
    }

    //重定向到
    @GetMapping("/toCart.html")
    public String toCart(Long skuId, Integer skuNum, Model model){
        CartInfo cartInfo = cartFeignClient.toCart(skuId);
        cartInfo.setSkuNum(skuNum);
        model.addAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }

    //去购物车结算（转发
    @GetMapping("/cart.html")
    public String cart(){
        //页面跳转
        return "cart/index";
    }

}
